package entity.model.relationships;

import entity.model.Entity;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public abstract class Relationship<T extends Annotation> {
    protected final Entity owner;       // entity who "has" the other
    protected final Entity possession;    // entity who "belongs" to the other

    protected final Field field;
    protected final T annotation;

    private final String ownerPropertyName;
    private final String possessionPropertyName;

    public Relationship(Entity owner, Entity possession, Field field, Class<T> annotationClass) {
        this.owner = owner;
        this.possession = possession;
        this.field = field;
        this.annotation = field.getAnnotation(annotationClass);
        ownerPropertyName = createOwnerPropertyName();
        possessionPropertyName = createPossessionPropertyName();
    }


    protected abstract String createPossessionPropertyName();

    protected abstract String createOwnerPropertyName();

    public String getOwnerPropertyName() {
        return ownerPropertyName;
    }

    public String getPossessionPropertyName() {
        return possessionPropertyName;
    }

    public Entity getOwner() {
        return owner;
    }

    public Entity getPossession() {
        return possession;
    }

    @Override
    public String toString() {
        String[] splits = getClass().getSimpleName().split("To");
        return String.format("%s %s has %s %s",
                splits[0],
                ownerPropertyName,
                splits[1],
                possessionPropertyName);
    }
}
